﻿using System;
using System.Collections.Generic;
using System.Text;
using RaisingStudio.Data.Common.Managers;
using RaisingStudio.Data.Common.Factories;

namespace RaisingStudio.Data.Common.Builders
{
    /// <summary>
    /// Command builder.
    /// </summary>
    public class CommandBuilder
    {
        #region static member
        #region Gets 参数
        public static bool GetSourceColumnNullMapping(System.Data.IDbDataParameter parameter)
        {
            return DataParameterManager.GetSourceColumnNullMapping(parameter);
        }

        /// <summary>
        /// Gets parameter through column names.
        /// </summary>
        /// <param name="parameters">Parameters.</param>
        /// <param name="columnName">Column name.</param>
        /// <returns>Parameters of columns.</returns>
        public static System.Data.IDbDataParameter GetOriginalMappingParameterByColumn(System.Data.IDataParameterCollection parameters, string columnName)
        {
            foreach (System.Data.IDbDataParameter parameter in parameters)
            {
                if ((parameter.SourceColumn == columnName) && (!GetSourceColumnNullMapping(parameter)) && (parameter.SourceVersion == System.Data.DataRowVersion.Original))
                {
                    return parameter;
                }
            }
            return null;
        }

        /// <summary>
        /// Gets parameter through column names.
        /// </summary>
        /// <param name="parameters">Parameters.</param>
        /// <param name="columnName">Column name.</param>
        /// <returns>Parameters of columns.</returns>
        public static System.Data.IDbDataParameter GetCurrentMappingParameterByColumn(System.Data.IDataParameterCollection parameters, string columnName)
        {
            foreach (System.Data.IDbDataParameter parameter in parameters)
            {
                if ((parameter.SourceColumn == columnName) && (!GetSourceColumnNullMapping(parameter)) && (parameter.SourceVersion == System.Data.DataRowVersion.Current))
                {
                    return parameter;
                }
            }
            return null;
        }

        /// <summary>
        /// Gets parameter through column names.
        /// </summary>
        /// <param name="parameters">Parameters.</param>
        /// <param name="columnName">Column name.</param>
        /// <returns>Parameters of columns.</returns>
        public static System.Data.IDbDataParameter GetNullMappingParameterByColumn(System.Data.IDataParameterCollection parameters, string columnName)
        {
            foreach (System.Data.IDbDataParameter parameter in parameters)
            {
                if ((parameter.SourceColumn == columnName) && (GetSourceColumnNullMapping(parameter)))
                {
                    return parameter;
                }
            }
            return null;
        }
        #endregion
        #endregion

        #region Common property 
        #region
        private bool checkConcurrency = false;
        /// <summary>
        /// Check concurrency.
        /// </summary>
        public bool CheckConcurrency
        {
            get
            {
                return this.checkConcurrency;
            }
            set
            {
                this.checkConcurrency = value;
            }
        }

        private bool getAutoIncrementValue = false;
        /// <summary>
        /// Gets the value of autoincrement.
        /// </summary>
        public bool GetAutoIncrementValue
        {
            get
            {
                return this.getAutoIncrementValue;
            }
            set
            {
                this.getAutoIncrementValue = value;
            }
        }

        private bool updateAutoIncrementValue = false;
        /// <summary>
        /// Update the value of autoincrement.
        /// </summary>
        public bool UpdateAutoIncrementValue
        {
            get
            {
                return this.updateAutoIncrementValue;
            }
            set
            {
                this.updateAutoIncrementValue = value;
            }
        }
        #endregion
        protected System.Data.IDbConnection connection;
        /// <summary>
        /// database connection.
        /// </summary>
        public System.Data.IDbConnection Connection
        {
            get
            {
                return this.connection;
            }
        }

        private DatabaseType _databaseType;
        /// <summary>
        /// Database type.
        /// </summary>
        public DatabaseType DatabaseType
        {
            get
            {
                return this._databaseType;
            }
            set
            {
                this._databaseType = value;
            }
        }

        private string _providerName;
        /// <summary>
        /// The name of provider.
        /// </summary>
        public string ProviderName
        {
            get
            {
                return this._providerName;
            }
            set
            {
                this._providerName = value;
            }
        }

        protected CommonCommandBuilder commonCommandBuilder;
        /// <summary>
        /// Common Command builder.
        /// </summary>
        public CommonCommandBuilder CommonCommandBuilder
        {
            get
            {
                return this.commonCommandBuilder;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        public CommandBuilder(System.Data.IDbConnection connection, System.Type type)
        {
            this.connection = connection;
            this.commonCommandBuilder = new CommonCommandBuilder(type);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        /// <param name="name">Data object name.</param>
        public CommandBuilder(System.Data.IDbConnection connection, System.Type type, string name)
        {
            this.connection = connection;
            this.commonCommandBuilder = new CommonCommandBuilder(type, name);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="xmlElement">Common Command builder.</param>
        public CommandBuilder(System.Data.IDbConnection connection, System.Xml.XmlElement xmlElement)
        {
            this.connection = connection;
            this.commonCommandBuilder = new CommonCommandBuilder(xmlElement);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="commonCommandBuilder">Common command builder.</param>
        public CommandBuilder(System.Data.IDbConnection connection, CommonCommandBuilder commonCommandBuilder)
        {
            this.connection = connection;
            this.commonCommandBuilder = commonCommandBuilder;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        public CommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, System.Type type)
        {
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.commonCommandBuilder = new CommonCommandBuilder(type);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        /// <param name="name">Data object name.</param>
        public CommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, System.Type type, string name)
        {
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.commonCommandBuilder = new CommonCommandBuilder(type, name);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="xmlElement">Common Command builder.</param>
        public CommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, System.Xml.XmlElement xmlElement)
        {
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.commonCommandBuilder = new CommonCommandBuilder(xmlElement);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="commonCommandBuilder">Common command builder.</param>
        public CommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, CommonCommandBuilder commonCommandBuilder)
        {
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.commonCommandBuilder = commonCommandBuilder;
        }
        #endregion
        
        /// <summary>
        /// Create Command.
        /// </summary>
        /// <returns>Command.</returns>
        protected virtual System.Data.IDbCommand CreateCommand()
        {
            return this.connection.CreateCommand();
        }

        /// <summary>
        /// Gets the Mapping coommand.
        /// </summary>
        /// <param name="commandCommand">Common Command.</param>
        /// <returns>the Mapping coommand.</returns>
        public virtual System.Data.IDbCommand GetMappingCommand(RaisingStudio.Data.CommonCommand commandCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMappingCommand(commandCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        #region Select 
        /// <summary>
        /// Gets "Select"  Command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public virtual System.Data.IDbCommand GetSelectCommand(out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetSelectCommand(out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets "Select"  Command.
        /// </summary>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public virtual System.Data.IDbCommand GetSelectCommand(string[] columnNames, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetSelectCommand(columnNames, out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets "Select"  Command.
        /// </summary>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public virtual System.Data.IDbCommand GetSelectCommand(CommonCommand conditionCommand, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetSelectCommand(conditionCommand, out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets "Select"  Command.
        /// </summary>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public virtual System.Data.IDbCommand GetSelectCommand(string[] columnNames, CommonCommand conditionCommand, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetSelectCommand(columnNames, conditionCommand, out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        #endregion
        #region Delete 
        /// <summary>
        /// Gets the "Delete" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>the "Delete" command.</returns>
        public System.Data.IDbCommand GetDeleteCommand(System.Data.Common.DataTableMapping dataTableMapping)
        {
            return GetDeleteCommand(dataTableMapping, this.checkConcurrency);
        }

        /// <summary>
        /// Gets the "Delete" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <param name="checkConcurrency">Check concurrency.</param>
        /// <returns>the "Delete" command.</returns>
        public System.Data.IDbCommand GetDeleteCommand(System.Data.Common.DataTableMapping dataTableMapping, bool checkConcurrency)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetDeleteCommand(dataTableMapping, checkConcurrency);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets the "Delete" command.
        /// </summary>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>the "Delete" command.</returns>
        public System.Data.IDbCommand GetDeleteCommand(CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetDeleteCommand(conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets the "Delete" command.
        /// </summary>
        /// <returns>the "Delete" command.</returns>
        public System.Data.IDbCommand GetDeleteCommand()
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetDeleteCommand();
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        #endregion
        #region Insert 
        /// <summary>
        /// Gets the "Insert" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>the "Insert" command.</returns>
        public System.Data.IDbCommand GetInsertCommand(System.Data.Common.DataTableMapping dataTableMapping)
        {
            return GetInsertCommand(dataTableMapping, this.getAutoIncrementValue);
        }

        /// <summary>
        /// Gets the "Insert" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <param name="getAutoIncrementValue">Gets the value of autoincrement.</param>
        /// <returns>the "Insert" command.</returns>
        public System.Data.IDbCommand GetInsertCommand(System.Data.Common.DataTableMapping dataTableMapping, bool getAutoIncrementValue)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetInsertCommand(dataTableMapping, getAutoIncrementValue);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        #endregion
        #region Update 
        /// <summary>
        /// Gets the "Update" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>the "Update" command.</returns>
        public System.Data.IDbCommand GetUpdateCommand(System.Data.Common.DataTableMapping dataTableMapping)
        {
            return GetUpdateCommand(dataTableMapping, this.updateAutoIncrementValue, this.checkConcurrency);
        }

        /// <summary>
        /// Gets the "Update" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <param name="updateAutoIncrementValue">Update the value of autoincrement.</param>
        /// <param name="checkConcurrency">Check concurrency.</param>
        /// <returns>the "Update" command.</returns>
        public System.Data.IDbCommand GetUpdateCommand(System.Data.Common.DataTableMapping dataTableMapping, bool updateAutoIncrementValue, bool checkConcurrency)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetUpdateCommand(dataTableMapping, updateAutoIncrementValue, checkConcurrency);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets the "Update" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <param name="updateAutoIncrementValue">Update the value of autoincrement.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>the "Update" command.</returns>
        public System.Data.IDbCommand GetUpdateCommand(System.Data.Common.DataTableMapping dataTableMapping, bool updateAutoIncrementValue, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetUpdateCommand(dataTableMapping, updateAutoIncrementValue, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        #endregion

        #region Normal Functions
        /// <summary>
        /// The item count in collection.
        /// </summary>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetDataCountCommand()
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetDataCountCommand();
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The item count in collection.
        /// </summary>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetDataCountCommand(CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetDataCountCommand(conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The sum value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetSumDataCommand(string columnName)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetSumDataCommand(columnName);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The sum value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetSumDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetSumDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The avg value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetAvgDataCommand(string columnName)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetAvgDataCommand(columnName);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The avg value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetAvgDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetAvgDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The max value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetMaxDataCommand(string columnName)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMaxDataCommand(columnName);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The max value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetMaxDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMaxDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The min vlaue.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetMinDataCommand(string columnName)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMinDataCommand(columnName);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The min vlaue.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetMinDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMinDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        #endregion

        /// <summary>
        /// Gets the "Exists" command.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>the "Exists" command.</returns>
        public virtual System.Data.IDbCommand GetExistsCommand(System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetExistsCommand(dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets the "Select" command include condition.
        /// </summary>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public System.Data.IDbCommand GetConditionSelectCommand(out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetConditionSelectCommand(out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets the "Select" command include condition.
        /// </summary>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public System.Data.IDbCommand GetConditionSelectCommand(string[] columnNames, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetConditionSelectCommand(columnNames, out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
       
        /// <summary>
        /// Create Condition Command.
        /// </summary>
        /// <param name="dataTableMapping">mapping.</param>
        /// <returns>Condition Command.</returns>
        public virtual System.Data.IDbCommand GetNormalConditionCommand(System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetNormalConditionCommand(dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        #region View Mode
        /// <summary>
        /// Gets "Select"  Command.
        /// </summary>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public System.Data.IDbCommand GetViewSelectCommand(CommonCommand conditionCommand, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetViewSelectCommand(conditionCommand, out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets "Select"  Command.
        /// </summary>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <param name="dataTableMapping">Data table mapping.</param>
        /// <returns>"Select"  Command.</returns>
        public System.Data.IDbCommand GetViewSelectCommand(string[] columnNames, CommonCommand conditionCommand, out System.Data.Common.DataTableMapping dataTableMapping)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetViewSelectCommand(columnNames, conditionCommand, out dataTableMapping);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
 
        #region Normal Functions
        /// <summary>
        /// The item count in collection.
        /// </summary>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetViewDataCountCommand(CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetDataCountCommand(conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The sum value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetViewSumDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetSumDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The avg value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetViewAvgDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetAvgDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The max value.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetViewMaxDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMaxDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// The min vlaue.
        /// </summary>
        /// <param name="columnName">Column name.</param>
        /// <param name="conditionCommand">"Condition" Command.</param>
        /// <returns>Command.</returns>
        public virtual System.Data.IDbCommand GetViewMinDataCommand(string columnName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMinDataCommand(columnName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        #endregion
        #endregion

        public virtual DatabaseType GetDatabaseType()
        {
            if ((this._databaseType == DatabaseType.OTHER) && (!ProviderManager.Instance.ProviderExists(this._providerName)))
            {
                DatabaseType databaseType = ConnectionFactory.GetDatabaseType(this.connection);
                return databaseType;
            }
            return this._databaseType;
        }

        protected virtual System.Data.IDbCommand ConvertCommand(CommonCommand commonCommand)
        {
            DatabaseType databaseType = GetDatabaseType();
            return CommandConverter.Convert(commonCommand, this.connection, databaseType, this._providerName);
        }


        public System.Data.IDbCommand GetUpdateCommand(System.Data.Common.DataTableMapping dataTableMapping, CommonCommand conditionCommand)
        {
            return GetUpdateCommand(dataTableMapping, true, conditionCommand);
        }
    }
}
